# -*- codeing = utf-8 -*-
# @Time : 2021/2/22 18:19
# @Atuthor: 朱朱
# @File: 047冒泡排序优化.py
# @Software: PyCharm
nums = [6, 5, 3, 1, 8, 7, 2, 4]

count = 0
j = 0
while j<len(nums)-1:
    flag = True # 设置flag,假设没有交换
    i = 0
    while i<len(nums)-1-j:
        count+=1
        if nums[i]>nums[i+1]:
            flag=False # 进行了交换,flag为false
            nums[i],nums[i+1] = nums[i+1],nums[i]
        i+=1
    j+=1
    if flag:
        break
print(j)
print(nums)
print('比较了%d次'%count)